var base = require('/base_info.js');
var utils = require('/utils/index.js');
var common = require('/utils/common.js');
Page({
  data: {
    orderInfo: {},
    imgUrl: base.imgUrl,
    order_id: 0,
    sign: false
  },
  // 获取数据
  getData () {
    let that = this;
    my.showLoading();
    utils.getData('SingleOrderDetails', {
      user_id: my.getStorageSync({
        key: 'uid', // 缓存数据的key
      }).data,
      order_id: that.data.order_id
    }, (res) => {
      my.hideLoading();
      console.log(res.data);
      res.data.data.time = common.transTime(res.data.data.time * 1000);
      res.data.data.arrival_times = res.data.data.arrival_time.slice(0, 10);
      res.data.data.departure_time = res.data.data.departure_time.slice(0, 10);
      res.data.data.CNstatus = that.orderStatus(res.data.data.status * 1);
      res.data.data.timeDown = 0;
      that.setData({
        orderInfo: res.data.data
      });
    }, (err) => {
      my.hideLoading();
      console.log(err);
    });
  },
  // 订单状态转换
  orderStatus(val) {
    let result = '';
    switch (val) {
      case 1:
        result = '待支付';
        break;
      case 2:
        result = '未入住';
        break;
      case 3:
        result = '已取消';
        break;
      case 4:
        result = '已完成';
        break;
      case 9:
        result = '待评价';
        break;
      case 6:
        result = '退款中';
        break;
      case 5:
        result = '待消费';
        break;
      case 7:
        result = '已退款';
        break;
    };
    return result;
  },
  // 取消申请退款
  cancelRefund () {
    let that = this;
    my.showLoading();
    utils.getData('SingleCancelApplyRefund', {
      order_id: that.data.order_id
    }, (res) => {
      my.hideLoading();
      if (res.data.errno == 1) {
        my.alert({
          title: `已成功取消申请`,
          success: () => {
            that.getData();
          }
        }); 
        return false;
      }
      my.alert({
        title: `${res.data.message}`,
      });
    }, (err) => {
      my.hideLoading();
      my.alert({
        title: `网络错误，请稍后再试`,
      });
    });
  }, 
  // 删除订单
  delete (e) {
    let that = this;
    my.confirm({
      title: '',
      content: '订单一旦删除将无法恢复,是否确定删除该订单？',
      confirmButtonText: '是',
      cancelButtonText: '否',
      success: (result) => {
        if (result.confirm) {
          my.showLoading();
          utils.getData('SingleOrderDel', {
            order_id: that.data.order_id
          }, (res) => {
            my.hideLoading();
            console.log(res.data.errno == 1);
            if (res.data.errno == 1) {
              my.alert({
                title: `订单已成功删除`,
                success: () => {
                  that.getData();
                  my.navigateBack({});
                }
              });
              return false;
            }
            my.alert({
              title: `${res.data.message}`,
            });
          }, (err) => {
            my.hideLoading();
            my.alert({
              title: `网络错误，请稍后再试`,
            });
          });
        }
      },
    });
  },
  // 取消订单
  cancel (e) {
    let that = this;
    my.confirm({
      title: '',
      content: '订单一旦取消将需要重新预订,是否确定取消该订单？',
      confirmButtonText: '是',
      cancelButtonText: '否',
      success: (result) => {
        if (result.confirm) {   
          my.showLoading();     
          utils.getData('SingleCancelOrder', {
            order_id: that.data.order_id
          }, (res) => {
            my.hideLoading();
            if (res.data.errno == 1) {
              my.alert({
                title: `订单已成功取消`,
                success: () => {
                  that.getData();
                }
              });
              return false;
            }
            my.alert({
              title: `${res.data.message}`,
            });
          }, (err) => {
            my.hideLoading();
            my.alert({
              title: `网络错误，请稍后再试`,
            });
          });
        }
      },
    });
  },
  // 去支付
  pay (e) {
    // 调用支付
    let that = this;
    my.showLoading();
    new Promise((resolve, reject) => {
      utils.getData('SingleOrderStatus', {
        order_id: that.data.order_id
      }, (res) => {
        console.log(res.data.data.status)
        if (res.data.data.status * 1 == 1) {
          resolve(1);
          return false;
        };
        reject(1);
      }, err => {
        reject(1);
      })
    }).then((data) => {
      console.log('成功');
      // 如果是预授权
      if (e.currentTarget.dataset.method == 2) {
        my.tradePay({
          orderStr: e.currentTarget.dataset.orderStr,
          success: (res) => {
            my.hideLoading();
            if (res.resultCode == 9000) {
              my.alert({
                content: '支付成功',
                success: () => {
                  that.getData();
                }
              });
            } else {
              my.alert({
                content: '支付失败'
              });
            }
          },
          fail: (res) => {
            my.hideLoading();
            my.alert({
              content: '支付失败',
            });
          }
        });
        return false;
      };
      my.tradePay({
        tradeNO: that.data.orderInfo.trade_no,
        success: (res) => {
          my.hideLoading();
          if (res.resultCode == 9000) {
            my.alert({
              content: '支付成功',
              success: () => {
                that.getData();
              }
            });
          } else {
            my.alert({
              content: '支付失败'
            });
          }
        },
        fail: (res) => {
          my.hideLoading();
          my.alert({
            content: '支付失败',
          });
        }
      });
    }, (err) => {
      my.hideLoading();
      my.alert({
        content: '该订单不可以支付' 
      });
    })
  },
  // 申请退款
  refund (e) {
    // 调转到申请退款页面
    my.navigateTo({
      url: './order-refund?id=' + this.data.order_id
    });
  },
  // 去评价
  comment (e) {
    // 跳转到去评价页面
    my.navigateTo({
      url: './order-comment?id=' + e.currentTarget.dataset.id
    })
  },
  // 交易明细
  treadDetail () {
    console.log('sign');
    let that = this;
    that.setData({
      sign: !that.data.sign
    })
  },
  // 阻止冒泡
  contentClick () {},
  onLoad(query) {
    // 页面加载
    let that = this;
    this.setData({
      order_id: query.id
    });
    this.getData();
    setInterval(function () {
      var data = that.data.orderInfo;
      data.timeDown = common.timeDown(data.time);
      if (data.timeDown == 0 && data.status == 1) {
        data.status = 3;
        data.CNstatus = '已取消';
      };
      that.setData({
        orderInfo: data
      })
    }, 1000)
  },
  onReady() {
    // 页面加载完成
  },
  onShow() {
    // 页面显示
    this.getData();
  },
  onHide() {
    // 页面隐藏
  },
  onUnload() {
    // 页面被关闭
  },
  onTitleClick() {
    // 标题被点击
  },
  onPullDownRefresh() {
    // 页面被下拉
  },
  onReachBottom() {
    // 页面被拉到底部
  },
  onShareAppMessage() {
    // 返回自定义分享信息
    return {
      title: my.getStorageSync({
        key: 'hotel_name', // 缓存数据的key
      }).data,
      desc: my.getStorageSync({
        key: 'my_app_description', // 缓存数据的key
      }).data,
      path: 'pages/index/index?share=true',
      success: function (e) {
        console.log(e);
      }
    };
  },
});
